<HTML>
<HEAD>
<TITLE>distance()</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Rogue Wave Standard Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="deque.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="divides.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Rogue Wave C++ Standard Library Reference Guide</B></DIV>
<H2>distance()</H2>
<P><B>Module:</B>&nbsp;&nbsp;Standard C++ Library&nbsp;&nbsp;&nbsp;<B>Library:</B>&nbsp;&nbsp;<A HREF="2-8.html">Iterators</A></P>

<PRE><HR><B><I>Function</I></B><HR></PRE>

<UL>
<LI><A HREF="#sec1">Local Index</A></LI>
<LI><A HREF="#sec2">Summary</A></LI>
<LI><A HREF="#sec3">Synopsis</A></LI>
<LI><A HREF="#sec4">Description</A></LI>
<LI><A HREF="#sec5">Example</A></LI>
<LI><A HREF="#sec6">Warnings</A></LI>
<LI><A HREF="#sec7">See Also</A></LI>
<LI><A HREF="#sec8">Standards Conformance</A></LI>
</UL>
<A NAME="sec1"><H3>Local Index</H3></A>
No Entries
<A NAME="sec2"><H3>Summary</H3></A>
<P>A function that computes the distance between two iterators</P>
<A NAME="sec3"><H3>Synopsis</H3></A>

<PRE>#include &lt;iterator&gt;

namespace std {
  template &lt;class ForwardIterator&gt;
  iterator_traits&lt;ForwardIterator&gt;::difference_type
  distance(ForwardIterator start,
           ForwardIterator finish);

  template &lt;class ForwardIterator, class Distance&gt;
  void distance(ForwardIterator start,
                ForwardIterator finish,
                Distance&amp; n);
}
</PRE>
<A NAME="sec4"><H3>Description</H3></A>
<P>The <SAMP>distance()</SAMP> function template computes the distance between two iterators. The first version returns that value, while the second version increments <SAMP>n</SAMP> by that value. The last iterator must be reachable from the first iterator. </P>
<P>Note that the second version of this function is obsolete. It is included for backward compatibility and to accommodate compilers that do not support partial specialization. The first version of the function is not available with compilers that do not support partial specialization, since it depends on <B><I><A HREF="iterator-traits.html">iterator_traits</A></I></B>, which itself depends on that particular language feature.</P>
<A NAME="sec5"><H3>Example</H3></A>

<UL><PRE>//
// distance.cpp
//

#include &lt;iterator&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;



int main ()
{
    // Typedefs for convenience.
    typedef std::vector&lt;int, std::allocator&lt;int&gt; &gt; vector;

    typedef std::ostream_iterator&lt;vector::value_type, char,
                              std::char_traits&lt;char&gt; &gt; os_iter;

    // Initialize a vector using an array.
    const vector::value_type arr [] = { 3, 4, 5, 6, 7, 8 };
    const vector v (arr + 0, arr + sizeof arr/ sizeof *arr);

    // Declare a vector iterator, s.b. a ForwardIterator.
    vector::const_iterator it = v.begin () + 3;

    // Output the original vector.
    std::cout &lt;&lt; "For the vector: ";
    std::copy (v.begin (), v.end (), os_iter (std::cout, " "));
    std::cout &lt;&lt; std::endl;

    std::cout &lt;&lt; "\nWhen the iterator is initialized "
              &lt;&lt; "to point to " &lt;&lt; *it &lt;&lt; std::endl;

    // Compute the distance of it from the first element.
    vector::difference_type dist = 0;
    std::distance (v.begin (), it, dist);

    std::cout &lt;&lt; "The distance between the beginning and "
              &lt;&lt; *it &lt;&lt; " is " &lt;&lt; dist &lt;&lt; std::endl;

    return 0;
}


Program Output:
</PRE></UL>
<UL><PRE>For the vector: 3 4 5 6 7 8 

When the iterator is initialized to point to 6
The distance between the beginning and 6 is 3
</PRE></UL>
<A NAME="sec6"><H3>Warnings</H3></A>
<P>If your compiler does not support partial specialization, you can't use the version of <SAMP>distance()</SAMP> that returns the distance. Instead, you must use the version that increments a reference parameter.</P>
<A NAME="sec7"><H3>See Also</H3></A>
<P><A HREF="sequences.html">Sequences</A>, <A HREF="randomaccessiterators.html">Random Access Iterators</A></P>
<A NAME="sec8"><H3>Standards Conformance</H3></A>
<P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 24.3.4</I></P>

<BR>
<HR>
<A HREF="deque.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="divides.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A></BODY>
</HTML>
